import React, {Component} from 'react';
import {Input} from 'antd';
import EditTableCellCss from '../../css/EditTableCellCss.css';


class EditTableCell extends Component {
    constructor(props) {
        super(props);
        this.state = {
            value: this.props.value,
            editTable: this.props.editTable || false
        }
    }

    componentWillReceiveProps(nextProps) {
        let editTable = this.state.editTable;
        if (nextProps.editTable !== editTable) {
            this.setState({
                editTable: nextProps.editTable
            });
            if (nextProps.editTable) {
                this.catchValue = this.state.value;
            }
        }
        if (nextProps.status && nextProps.status !== this.props.status) {
            if (nextProps.status === 'save') {
                this.props.onChange(this.state.value);
            } else if (nextProps.state === 'cancel') {
                this.setValue({
                    value: this.catchValue
                });
                this.props.onChange(this.catchValue);
            }
        }
    }

    shouldComponentUpdate(nextProps, nextState) {
        return nextProps.editTable !== this.state.editTable || nextState.value !== this.state.value;
    }

    handleChange(e) {
        const value = e.target.value;
        this.setState({
            value: value
        });
        console.log("cellHandleChange")
    }

    render() {
        const {value, editTable} = this.state;
        return (
            <div>
                {
                    editTable ?
                        <div>
                            <Input
                                value={value}
                                onChange={e => this.handleChange(e)}
                            />
                        </div>
                        :
                        <div className="editable-row-text">
                            {value.toString() || ' '}
                        </div>
                }
            </div>
        );
    }
}

export default EditTableCell;
